package com.lg.cms.controller.admin.rotation;

import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lg.cms.entity.Article;
import com.lg.cms.entity.Rotation;
import com.lg.cms.service.ArticleService;
import com.lg.cms.service.CategoryService;
import com.lg.cms.service.RotationService;
import com.lg.cms.vo.LayerTableVO;
import com.lg.cms.vo.admin.RotationArticleVO;
import com.lg.cms.vo.admin.SuperRotationVO;
import com.lg.cms.vo.news.CategoryVO;
import com.sun.javaws.IconUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import sun.plugin.javascript.navig4.Layer;

import javax.servlet.http.HttpServletRequest;
import java.sql.Wrapper;
import java.util.ArrayList;
import java.util.List;

/**
 * @Description: 轮播图管理视图
 * @Author: wuyuhang
 * @create: 2020-11-09 09:24
 */
@Controller
@RequestMapping("/admin/rotation")
public class RotationController {
    @Autowired
    ArticleService articleService;
    @Autowired
    RotationService rotationService;
    @Autowired
    CategoryService categoryService;

    @GetMapping("/get")
    public String get(HttpServletRequest request, Model model){
        model.addAttribute("request", request);
        model.addAttribute("categoryListChildren", categoryService.getList());
        model.addAttribute("categoryList", categoryService.list());
        return "/admin/interaction_management/carouselCategory";
    }

    /**
     * 查询分类列表, 带二级分类
     * @return
     */
    @GetMapping("/get_category_page")
    @ResponseBody
    public List<CategoryVO> getCategoryList(){
        return categoryService.getList();
    }
    /**
     * 获取文章简单列表, 带分页, 带搜索参数(标题, 描述, 分类id)
     * @param categoryId
     * @param title
     * @param description
     * @return
     */
    @GetMapping("/super_rotation_get_article_page")
    @ResponseBody
    public LayerTableVO<RotationArticleVO> superRotationGetArticlePage(@RequestParam("page") Integer current, @RequestParam("limit") Integer limit, Integer categoryId, String title, String description){
        Page page = new Page(current, limit);
        LayerTableVO<RotationArticleVO> layerTableVO = new LayerTableVO<>();
        QueryWrapper<Article> wrapper = new QueryWrapper<>();
        if(!StringUtils.isEmpty(title)){
            wrapper.like("title", title);
        }
        if(!StringUtils.isEmpty(description)){
            wrapper.like("description", description);
        }
        if(categoryId != null){
            wrapper.like("category_id", categoryId);
        }
        articleService.page(page, wrapper);

        List<RotationArticleVO> voList = new ArrayList<>();
        for(Object article: page.getRecords()){
            voList.add(rotationService.toRotationArticleVO((Article) article));
        }
        layerTableVO.setCode(0);
        layerTableVO.setCount(page.getTotal());
        layerTableVO.setMsg("");
        layerTableVO.setData(voList);
        return layerTableVO;
    }


    /**
     * 获取轮播图列表
     * @param page
     * @return
     */
    @GetMapping("/get_page")
    @ResponseBody
    public LayerTableVO<SuperRotationVO> getPage(Page page){
        LayerTableVO<SuperRotationVO> layerTableVO = new LayerTableVO<>();
        rotationService.page(page, new QueryWrapper<Rotation>().orderByAsc("sort"));
        List<SuperRotationVO> superRotationVOList = new ArrayList<>();
        for(Object obj: page.getRecords()){
            superRotationVOList.add(rotationService.toSuperRotationVO((Rotation)obj));
        }
        layerTableVO.setData(superRotationVOList);
        layerTableVO.setMsg("");
        layerTableVO.setCount(page.getTotal());
        layerTableVO.setCode(0);
        return layerTableVO;
    }

    /**
     * 获取单条
     * @param id
     * @return
     */
    @GetMapping("/get/{id}")
    @ResponseBody
    public SuperRotationVO get(@PathVariable Integer id){
        Rotation rotation = rotationService.getById(id);
        if(rotation != null) {
            return rotationService.toSuperRotationVO(rotation);
        }
            return null;
    }

    @PostMapping("/add")
    @ResponseBody
    public R add(Rotation rotation){
        if(!rotation.checkData()){
            return R.failed("请输入正确数据");
        }
        boolean result = rotationService.save(rotation);
        if(!result){
            return R.failed("新增轮播图失败");
        }
        return R.ok(rotation);
    }

    /**
     * 修改轮播图
     * @param rotation
     * @return
     */
    @PutMapping("/update")
    @ResponseBody
    public R update(Rotation rotation){
        if(rotation.getId() == null){
            return R.failed("id为空");
        }
        if(!rotation.checkData()){
            return R.failed("请输入正确数据");
        }
        Rotation oldRotation = rotationService.getById(rotation.getId());
        if (oldRotation != null){
            oldRotation.setArticleId(rotation.getArticleId());
            oldRotation.setCoverImage(rotation.getCoverImage());
            oldRotation.setSort(rotation.getSort());
            boolean result = rotationService.updateById(oldRotation);
            if(!result){
                return R.failed("修改轮播图失败");
            }
            return R.ok(null);
        }else{
            return R.failed("轮播图不存在");
        }

    }

    /**
     * 删除轮播图
     * @param id
     * @return
     */
    @DeleteMapping("/delete")
    @ResponseBody
    public R delete(Integer id){
        if(id == null){
            return R.failed("id不能为空");
        }
        boolean result = rotationService.removeById(id);
        if(!result){
            return R.failed("删除轮播图失败");
        }else{
            return R.ok(null);
        }
    }

}